package com.mdd.admin.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.course.ICourseMaterialService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.course.CourseMaterialParam;
import com.mdd.admin.vo.course.CourseMaterialListVo;
import com.mdd.admin.vo.course.CourseMaterialDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.course.CourseMaterial;
import com.mdd.common.mapper.course.CourseMapper;
import com.mdd.common.mapper.course.CourseMaterialMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 课件资料实现类
 * @author panweiliang
 */
@Service
public class CourseMaterialServiceImpl implements ICourseMaterialService {
        
    @Resource
    CourseMaterialMapper courseMaterialMapper;

    @Resource
    CourseMapper courseMapper;

    /**
     * 课件资料列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<CourseMaterialListVo>
     */
//    @Override
//    public PageResult<CourseMaterialListVo> list(PageParam pageParam, Map<String, String> params) {
//        Integer page  = pageParam.getPageNo();
//        Integer limit = pageParam.getPageSize();
//
//        QueryWrapper<CourseMaterial> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//
//        courseMaterialMapper.setSearch(queryWrapper, params, new String[]{
//            "=:courseId@course_id:int",
//            "=:content:str",
//            "=:link:str",
//            "=:code:str",
//            "=:isLink@is_link:int",
//        });
//
//        IPage<CourseMaterial> iPage = courseMaterialMapper.selectPage(new Page<>(page, limit), queryWrapper);
//
//        List<CourseMaterialListVo> list = new LinkedList<>();
//        for(CourseMaterial item : iPage.getRecords()) {
//            CourseMaterialListVo vo = new CourseMaterialListVo();
//            BeanUtils.copyProperties(item, vo);
//            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
//            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
//            list.add(vo);
//        }
//
//        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
//    }

    /**
     * 课件资料详情
     *
     * @author panweiliang
     * @param id 主键参数
     * @return CourseMaterial
     */
    @Override
    public CourseMaterialDetailVo detail(Integer id) {
        CourseMaterial model = courseMaterialMapper.selectOne(
                new QueryWrapper<CourseMaterial>()
                    .eq("course_id", id)
                    .last("limit 1"));
        if (model != null) {
            CourseMaterialDetailVo vo = new CourseMaterialDetailVo();
            BeanUtils.copyProperties(model, vo);
            vo.setContent(UrlUtil.toAbsoluteUrl(vo.getContent()));
            return vo;
        }
        return null;
    }

    /**
     * 课件资料新增
     *
     * @author panweiliang
     * @param courseMaterialParam 参数
     */
//    @Override
//    public void add(CourseMaterialParam courseMaterialParam) {
//        CourseMaterial model = new CourseMaterial();
//        model.setCourseId(courseMaterialParam.getCourseId());
//        model.setContent(UrlUtil.toRelativeUrl(courseMaterialParam.getContent()));
//        model.setLink(courseMaterialParam.getLink());
//        model.setCode(courseMaterialParam.getCode());
//        model.setIsLink(courseMaterialParam.getIsLink());
//        model.setCreateTime(System.currentTimeMillis() / 1000);
//        model.setUpdateTime(System.currentTimeMillis() / 1000);
//        courseMaterialMapper.insert(model);
//    }

    /**
     * 课件资料新增
     *
     * @author panweiliang
     * @param courseMaterialParam 参数
     */
    @Override
    public void addOrUpdate(CourseMaterialParam courseMaterialParam) {

        Course course = courseMapper.selectOne(
                new QueryWrapper<Course>()
                        .eq("id", courseMaterialParam.getCourseId())
                        .last("limit 1"));
        Assert.notNull(course, "课程数据不存在!");
        CourseMaterial model = courseMaterialMapper.selectOne(
                new QueryWrapper<CourseMaterial>()
                        .eq("course_id",  courseMaterialParam.getCourseId())
                        .last("limit 1"));

        if (model == null) {
            model = new CourseMaterial();
            model.setCourseId(courseMaterialParam.getCourseId());
            model.setContent(UrlUtil.toRelativeUrl(courseMaterialParam.getContent()));model.setFileName(courseMaterialParam.getFileName());
            model.setLink(courseMaterialParam.getLink());
            model.setCode(courseMaterialParam.getCode());
            model.setIsLink(courseMaterialParam.getIsLink());
            model.setCreateTime(System.currentTimeMillis() / 1000);
            model.setUpdateTime(System.currentTimeMillis() / 1000);
            courseMaterialMapper.insert(model);
        } else {
            model.setCourseId(courseMaterialParam.getCourseId());
            model.setFileName(courseMaterialParam.getFileName());
            model.setContent(UrlUtil.toRelativeUrl(courseMaterialParam.getContent()));
            model.setLink(courseMaterialParam.getLink());
            model.setCode(courseMaterialParam.getCode());
            model.setIsLink(courseMaterialParam.getIsLink());
            model.setCreateTime(System.currentTimeMillis() / 1000);
            model.setUpdateTime(System.currentTimeMillis() / 1000);
            courseMaterialMapper.updateById(model);
        }
    }

    /**
     * 课件资料编辑
     *
     * @author panweiliang
     * @param courseMaterialParam 参数
     */
//    @Override
//    public void edit(CourseMaterialParam courseMaterialParam) {
//        CourseMaterial model = courseMaterialMapper.selectOne(
//                new QueryWrapper<CourseMaterial>()
//                    .eq("id",  courseMaterialParam.getId())
//                    .last("limit 1"));
//
//        Assert.notNull(model, "数据不存在!");
//
//        model.setId(courseMaterialParam.getId());
//        model.setCourseId(courseMaterialParam.getCourseId());
//        model.setContent(courseMaterialParam.getContent());
//        model.setLink(courseMaterialParam.getLink());
//        model.setCode(courseMaterialParam.getCode());
//        model.setIsLink(courseMaterialParam.getIsLink());
//        model.setUpdateTime(System.currentTimeMillis() / 1000);
//        courseMaterialMapper.updateById(model);
//    }

    /**
     * 课件资料删除
     *
     * @author panweiliang
     * @param id 主键ID
     */
//    @Override
//    public void del(Integer id) {
//        CourseMaterial model = courseMaterialMapper.selectOne(
//                new QueryWrapper<CourseMaterial>()
//                    .eq("id", id)
//                    .last("limit 1"));
//
//        Assert.notNull(model, "数据不存在!");
//
//        courseMaterialMapper.delete(new QueryWrapper<CourseMaterial>().eq("id", id));
//    }

}
